package com.order.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.common.context.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("插入自动填充字段.....");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
        this.strictInsertFill(metaObject, "createUser", Long.class, UserContextHolder.getContext().getUsersId());
        this.strictInsertFill(metaObject, "updateUser", Long.class, UserContextHolder.getContext().getUsersId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("更新自动填充字段.....");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateUser", Long.class, UserContextHolder.getContext().getUsersId());
    }
}
